home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / threadheap.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-06-18  |  3.4 KB  |  126 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. //////////////////////////////////////////////////////////////////////////////
  22. //
  23. // Note: This file was modified by Crystal Decisions in June 2002.
  24. //
  25. //////////////////////////////////////////////////////////////////////////////
  26.  
  27.  
  28. #include <limits.h>
  29. #include <string.h>
  30.  
  31. #include "config.h"
  32.  
  33. #include "heap.h"
  34. #include "threadheap.h"
  35. #include "processheap.h"
  36.  
  37.  
  38. threadHeap::threadHeap (void)
  39.   : _pHeap (0)
  40. {}
  41.  
  42.  
  43. // malloc (sz):
  44. //   inputs: the size of the object to be allocated.
  45. //   returns: a pointer to an object of the appropriate size.
  46. //   side effects: allocates a block from a superblock;
  47. //                 may call sbrk() (via makeSuperblock).
  48.  
  49. void * threadHeap::malloc (const size_t size)
  50. {
  51.   const int sizeclass = sizeClass (size);
  52.   block * b = NULL;
  53.  
  54.   lock();
  55.  
  56.   // Look for a free block.
  57.   // We usually have memory locally so we first look for space in the
  58.   // superblock list.
  59.  
  60.   superblock * sb = findAvailableSuperblock (sizeclass, b, _pHeap);
  61.  
  62.   if (sb == NULL) {
  63.     
  64.     // We don't have memory locally.
  65.     // Try to get more from the process heap.
  66.     
  67.     assert (_pHeap);
  68.     sb = _pHeap->acquire ((int) sizeclass, this);
  69.     
  70.     // If we didn't get any memory from the process heap,
  71.     // we'll have to allocate our own superblock.
  72.     if (sb == NULL) {
  73.       sb = superblock::makeSuperblock (sizeclass, _pHeap);
  74.       if (sb == NULL) {
  75. #ifdef CRYSTAL_HOARD
  76.           unlock();
  77. #endif    
  78.     // We're out of memory!
  79.     return NULL;
  80.       }
  81. #if HEAP_LOG
  82.       // Record the memory allocation.
  83.       MemoryRequest m;
  84.       m.allocate ((int) sb->getNumBlocks() * (int) sizeFromClass(sb->getBlockSizeClass()));
  85.       _pHeap->getLog(getIndex()).append(m);
  86. #endif
  87. #if HEAP_FRAG_STATS
  88.       _pHeap->setAllocated (0, sb->getNumBlocks() * sizeFromClass(sb->getBlockSizeClass()));
  89. #endif
  90.     }
  91.     
  92.     // Get a block from the superblock.
  93.     b = sb->getBlock ();
  94.     assert (b != NULL);
  95.  
  96.     // Insert the superblock into our list.
  97.     insertSuperblock (sizeclass, sb, _pHeap);
  98.   }
  99.  
  100.   assert (b != NULL);
  101.   assert (b->isValid());
  102.   assert (sb->isValid());
  103.  
  104.  
  105. #if defined(CRYSTAL_HOARD) && defined(WIN32)
  106.   b->setActualSize(size);
  107. #endif
  108.  
  109.   b->markAllocated();
  110.  
  111. #if HEAP_LOG
  112.   MemoryRequest m;
  113.   m.malloc ((void *) (b + 1), align(size));
  114.   _pHeap->getLog(getIndex()).append(m);
  115. #endif
  116. #if HEAP_FRAG_STATS
  117.   b->setRequestedSize (align(size));
  118.   _pHeap->setAllocated (align(size), 0);
  119. #endif
  120.  
  121.   unlock();
  122.   
  123.   // Skip past the block header and return the pointer.
  124.   return (void *) (b + 1);
  125. }
  126.